package com.bootdo.order.controller;

import com.bootdo.common.utils.ExceptionUtils;
import com.bootdo.common.utils.R;
import com.bootdo.order.domain.OrderDO;
import com.bootdo.order.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Desc 订单
 * @date 2019/11/18
 *
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    private static final Logger logger = LoggerFactory.getLogger(OrderController.class);

    /**
     * 创建订单返回json字符串
     * @return
     */
    @PostMapping("/create")
    R createOrder(OrderDO order){
        try {
            Map<String, Object> map = orderService.retMercs(order);
            if (map == null){
                logger.error("==========下单异常=========>" + order.getOrderNo() + "===暂无收款户====");
                return R.error("暂无收款户");
            }
            //推送消息
            orderService.sendMsg(map);
            return R.ok(map);
        } catch (Exception e) {
            logger.error("==========下单异常=========>" + order.getOrderNo() + "===" + ExceptionUtils.getExceptionAllinformation(e));
            return R.error(e.getMessage());
        }
    }

    /**
     * 根据订单编号获取对应的订单信息
     * @return
     */
    @PostMapping("/queryOrder")
    R queryOrder(String orderId){
        try {
            OrderDO order = new OrderDO();
            order.setId(orderId);
            Map<String, String> map = orderService.queryOrder(order);
            return R.ok(map);
        } catch (Exception e) {
            logger.error("==========根据订单编号获取对应的订单信息异常=========>" + ExceptionUtils.getExceptionAllinformation(e));
            return R.error(-1, e.getMessage());
        }
    }

    @PostMapping("/query")
    R query(OrderDO order){
        try {
            //检验签名
            orderService.checkSign(order);
            Map<String, String> map = orderService.queryRet(order);
            return R.ok(map);
        } catch (Exception e) {
            logger.error("==========查询订单异常=========>" + ExceptionUtils.getExceptionAllinformation(e));
            return R.error(-1, e.getMessage());
        }
    }

    @PostMapping("/findMerchant")
    R findMerchant(){
        return R.ok(orderService.findMerchant());
    }

    @PostMapping("/createOrderParam")
    R createOrderParam(OrderDO order){
        try {
            return R.ok(orderService.createOrderParam(order));
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

}
